package com.lh.controller;


import com.lh.Service.GoodService;
import com.lh.Service.Impl.GoodServiceImpl;
import com.lh.bean.Good;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;


@Controller
@RequestMapping("/good")
public class GoodController {
    @Resource
    private GoodService goodService;

    /**
     * 所有手机型号
     * @return
     */
    @RequestMapping("/good")
    @ResponseBody
    private Good queryGood(String good_name){
        Good goodList = goodService.queryGood(good_name);
        return goodList;
    }

    /**
     * 品牌下的手机型号
     * @param brand_name
     * @return
     */
    @RequestMapping("/selectGoods")
    @ResponseBody
    public List<Good> selectGoods(String brand_name){
        return goodService.selectGoodsByBrandName(brand_name);
    }

    @RequestMapping("/price")
    public ModelAndView getPrice(String infoDetailList, String good_name, Model model){
        String[] temps = infoDetailList.split(",");
        int[] detailIds = new int[temps.length];
        for (int i = 0; i < temps.length; i++) {
            detailIds[i] = Integer.parseInt(temps[i]);
        }

////     1.获取价格
////        1.1goodsMapper获取此goodsName商品..
////        1.2goodsMapper获取所有细节对象discount的总和..
        Good good = goodService.queryGood(good_name);
        int goodDiscount = goodService.getGoodsDiscount(good.getGood_id(),detailIds);
        Double price = good.getGood_cost() - goodDiscount;
        if( price > good.getGood_cost()){
            price = good.getGood_cost();
        }
        if(price < good.getGood_min_price()){
            price = good.getGood_min_price();
        }
        model.addAttribute("price", price);
        model.addAttribute("good", good);
        model.addAttribute("detailIds", detailIds);
        ModelAndView modelAndView = new ModelAndView("html/price");
        return modelAndView;
    }


}
